// 签名
// var crypto = require('crypto');

// 腾讯证件识别签名
const certificate = () => {
    var secretId = 'AKIDf9NTIq77rKWR6TZOwDCuLVNyIL7cqoVu',
        secretKey = '91cwzpPyLTLyLTLyLTLyLSJwJQHsELCo',
        appid = '1309154477',
        pexpired = 86400,
        userid = 0;
    var now = parseInt(Date.now() / 1000),
        rdm = parseInt(Math.random() * Math.pow(2, 32)),
        plainText = 'a=' + appid + '&k=' + secretId + '&e=' + (now + pexpired) + '&t=' + now + '&r=' + rdm + userid + '&f=',
        // data = new Buffer(plainText, 'utf8'),
        // res = crypto.createHmac('sha1', secretKey).update(data).digest(), 
        // bin = Buffer.concat([res, data]);

        // 兼容.nvue不能require引入外部js(crypto)
        // 兼容.nvue不能直接解析Buffer
        data = new getApp().buffer(plainText, 'utf8'),
        res = getApp().crypto.createHmac('sha1', secretKey).update(data).digest(),
        bin = getApp().buffer.concat([res, data]);
        var sign = bin.toString('base64');
    return sign
}



// 腾讯人脸静态活体检测签名
const get_req_url = (params, endpoint) => {
    params['Signature'] = escape(params['Signature']);
    const url_strParam = sort_params(params)
    return "https://" + endpoint + "/?" + url_strParam.slice(1);
}
const formatSignString = (reqMethod, endpoint, path, strParam) => {
    let strSign = reqMethod + endpoint + path + "?" + strParam.slice(1);
    return strSign;
}
const sha1 = (secretKey, strsign) => {
    let signMethodMap = {
        'HmacSHA1': "sha1"
    };
    // let hmac = crypto.createHmac(signMethodMap['HmacSHA1'], secretKey || "");
    // return hmac.update(Buffer.from(strsign, 'utf8')).digest('base64')
    // 兼容.nvue不能require引入外部js(crypto)
    // 兼容.nvue不能直接解析Buffer 
    let hmac = getApp().crypto.createHmac(signMethodMap['HmacSHA1'], secretKey || "");
    return hmac.update(getApp().buffer.from(strsign, 'utf8')).digest('base64')
}
const sort_params = (params) => {
    let strParam = "";
    let keys = Object.keys(params);
    keys.sort();
    for (let k in keys) {
        //k = k.replace(/_/g, '.');
        strParam += ("&" + keys[k] + "=" + params[keys[k]]);
    }
    return strParam
}
const face = () => {
    // 密钥参数
    const SECRET_ID = "AKIDf9NTIq77rKWR6TZOwDCuLVNyIL7cqoVu"
    const SECRET_KEY = "91cwzpPyLTLyLTLyLTLyLSJwJQHsELCo"

    const endpoint = "cvm.tencentcloudapi.com"
    const Region = "ap-chengdu"
    const Version = "2017-03-12"
    const Action = "DescribeInstances"
    const Timestamp = Math.round(Date.now() / 1000)
    const Nonce = Math.round(Math.random() * 65535)
    // const Timestamp = 1465185768 // 时间戳 2016-06-06 12:02:48, 此参数作为示例，以实际为准、
    // const Nonce = 11886 // 随机正整数

    let params = {};
    params['Action'] = Action;
    params['InstanceIds.0'] = 'ins-09dx96dg';
    params['Limit'] = 20;
    params['Offset'] = 0;
    params['Nonce'] = Nonce;
    params['Region'] = Region;
    params['SecretId'] = SECRET_ID;
    params['Timestamp'] = Timestamp;
    params['Version'] = Version;

    // 1. 对参数排序,并拼接请求字符串
    strParam = sort_params(params)

    // 2. 拼接签名原文字符串
    const reqMethod = "GET";
    const path = "/";
    strSign = formatSignString(reqMethod, endpoint, path, strParam)
    // console.log('签名原文字符串', strSign)

    // 3. 生成签名串
    params['Signature'] = sha1(SECRET_KEY, strSign)
    // console.log('签名串', params['Signature'])

    // 4. 进行url编码并拼接请求url
    const req_url = get_req_url(params, endpoint)
    // console.log('请求url', req_url)

    return params['Signature']
}



module.exports = {
    certificate,
    face
}